package com.xscha.tableforge.service.impl;

import com.xscha.tableforge.model.entity.ViewMeta;
import com.xscha.tableforge.model.vo.PageReq;
import com.xscha.tableforge.model.vo.PageResult;
import com.xscha.tableforge.repository.ViewMetaRepository;
import com.xscha.tableforge.service.BaseService;
import com.xscha.tableforge.service.ViewMetaService;
import com.xscha.tableforge.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 视图元数据服务实现类
 */
@Service
public class ViewMetaServiceImpl extends BaseService<ViewMeta> implements ViewMetaService {

    private final ViewMetaRepository viewMetaRepository;

    @Autowired
    public ViewMetaServiceImpl(ViewMetaRepository viewMetaRepository) {
        this.viewMetaRepository = viewMetaRepository;
    }

    @Override
    public List<ViewMeta> findAllByTableId(String tableId) {
        return viewMetaRepository.findAllByTableId(tableId);
    }

    @Override
    public Optional<ViewMeta> findById(String id) {
        return viewMetaRepository.findById(id);
    }

    @Override
    public PageResult<ViewMeta> findPageByTableId(String tableId, PageReq pageRequest) {
		PageReq processedReq = processPageRequest(pageRequest);
        return viewMetaRepository.findPageByTableId(tableId, processedReq);
    }

    @Override
    @Transactional
    public ViewMeta createView(ViewMeta viewMeta, String userId) {
        viewMeta.setId(IdUtil.createViewId());
        viewMeta.setCreatedTime(LocalDateTime.now());
        viewMeta.setCreatedBy(userId);
        viewMeta.setVersion(1);
        return viewMetaRepository.save(viewMeta);
    }

    @Override
    @Transactional
    public ViewMeta updateView(ViewMeta viewMeta, String userId) {
        viewMeta.setLastModifiedTime(LocalDateTime.now());
        viewMeta.setLastModifiedBy(userId);
        viewMeta.setVersion(viewMeta.getVersion() + 1);
        return viewMetaRepository.update(viewMeta);
    }

    @Override
    @Transactional
    public void deleteView(String id, String userId) {
        viewMetaRepository.softDelete(id, userId);
    }

    @Override
    @Transactional
    public ViewMeta updateShareSettings(String id, boolean enableShare, String shareMeta, String userId) {
        ViewMeta viewMeta = viewMetaRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("视图不存在"));

        viewMeta.setEnableShare(enableShare);

        if (enableShare && (viewMeta.getShareId() == null || viewMeta.getShareId().isEmpty())) {
            viewMeta.setShareId(IdUtil.createShareId());
        }

        viewMeta.setShareMeta(shareMeta);
        viewMeta.setLastModifiedTime(LocalDateTime.now());
        viewMeta.setLastModifiedBy(userId);
        viewMeta.setVersion(viewMeta.getVersion() + 1);

        return viewMetaRepository.update(viewMeta);
    }

	@Override
	protected Class<ViewMeta> getEntityClass() {
		return ViewMeta.class;
	}
}